﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using PH.Model.Analyses;
using PH.Model.Analyses.Bureaus;
namespace PH.Data.Analyses
{
    public class RBCommand
    {
        /// <summary>
        /// 按学校分析
        /// </summary>
        /// <param name="sea"></param>
        /// <returns></returns>
        public static B4SchoolCollection GetRB4School(BAnalysisQuery sea)
        {
            var ret = new B4SchoolCollection();
            if (sea != null)
            {
                using (DBCentre db = new DBCentre())
                {
                    var bureau = db.DataContext.Bureaus.FirstOrDefault(p => p.Guid == sea.BGuid);
                    if (bureau != null)
                    {
                        var n = 0;
                        IQueryable<RB4School> q = db.DataContext.RB4School;
                        q = q.Where(p => p.ArchiveGuid == sea.ArchiveGuid);
                        var sid = bureau.SchoolInBureaux.Select(p => p.SchoolID).ToList();
                        q = q.Where(p => sid.Contains(p.SchoolID));
                        if (!string.IsNullOrEmpty(sea.Key))
                            q = q.Where(p => p.SchoolName == sea.Key || p.SchoolCode==sea.Key);
                        if (sea.GradeLevel > 0)
                            q = q.Where(p => p.GradeLevel == sea.GradeLevel);
                        q = q.OrderBy(p => p.SchoolCode);
                        if (sea.PageSize > 0)
                        {
                            n = q.Count();
                            q = q.Skip(sea.PageSize * sea.PageIndex).Take(sea.PageSize);
                        }
                        var lst = q.ToList();
                        ret = Convertor.ConvertCollection<B4SchoolModel, RB4School, B4SchoolCollection>(lst);
                        ret.TotalRecordsCount = n == 0 ? lst.Count : n;
                    }
                }
            }
            return ret;
        }
        /// <summary>
        /// 按生源地学校分析
        /// </summary>
        /// <param name="sea"></param>
        /// <returns></returns>
        public static B4OriSchoolCollection GetRB4OriSchool(BAnalysisQuery sea)
        {
            var ret = new B4OriSchoolCollection();
            if (sea != null)
            {
                using (DBCentre db = new DBCentre())
                {
                    var bureau = db.DataContext.Bureaus.FirstOrDefault(p => p.Guid == sea.BGuid);
                    if (bureau != null)
                    {
                        var n = 0;
                        IQueryable<RB4OriSchool> q = db.DataContext.RB4OriSchool;
                        q = q.Where(p => p.ArchiveGuid == sea.ArchiveGuid);
                        var sid = bureau.SchoolInBureaux.Select(p => p.SchoolID).ToList();
                        q = q.Where(p => sid.Contains(p.SchoolID));
                        if (!string.IsNullOrEmpty(sea.Key))
                            q = q.Where(p => p.SchoolName == sea.Key || p.SchoolCode == sea.Key);
                        if (sea.GradeLevel > 0)
                            q = q.Where(p => p.GradeLevel == sea.GradeLevel);
                        q = q.OrderBy(p => p.SchoolCode);
                        if (sea.PageSize > 0)
                        {
                            n = q.Count();
                            q = q.Skip(sea.PageSize * sea.PageIndex).Take(sea.PageSize);
                        }
                        var lst = q.ToList();
                        ret = Convertor.ConvertCollection<B4OriSchoolModel, RB4OriSchool, B4OriSchoolCollection>(lst);
                        ret.TotalRecordsCount = n == 0 ? lst.Count : n;
                    }
                }
            }
            return ret;
        }
        /// <summary>
        /// 按机构分析
        /// </summary>
        /// <param name="sea"></param>
        /// <returns></returns>
        public static B4BureauCollection GetRB4Bureau(BAnalysisQuery sea)
        {
            var ret = new B4BureauCollection();
            if (sea != null)
            {
                using (DBCentre db = new DBCentre())
                {
                    var bureau = db.DataContext.Bureaus.FirstOrDefault(p => p.Guid == sea.BGuid);
                    if (bureau != null)
                    {
                        var n = 0;
                        IQueryable<RB4Bureau> q = db.DataContext.RB4Bureau;
                        q = q.Where(p => p.ArchiveGuid == sea.ArchiveGuid);
                        q = q.Where(p => p.PID == bureau.ID || p.BureauID == bureau.ID);
                        
                        if (!string.IsNullOrEmpty(sea.Key))
                            q = q.Where(p => p.EduName == sea.Key || p.Code == sea.Key);
                        if (sea.GradeLevel > 0)
                            q = q.Where(p => p.GradeLevel == sea.GradeLevel);
                        q = q.OrderBy(p => p.Code);
                        if (sea.PageSize > 0)
                        {
                            n = q.Count();
                            q = q.Skip(sea.PageSize * sea.PageIndex).Take(sea.PageSize);
                        }
                        var lst = q.ToList();
                        ret = Convertor.ConvertCollection<B4BureauModel, RB4Bureau, B4BureauCollection>(lst);
                        ret.TotalRecordsCount = n == 0 ? lst.Count : n;
                    }
                }
            }
            return ret;
        }
        /// <summary>
        /// 机构分析样本采集进度
        /// </summary>
        /// <param name="sea"></param>
        /// <returns></returns>
        public static B4SampleBureauCollection GetRB4SampleBureau(BAnalysisQuery sea)
        {
            var ret = new B4SampleBureauCollection();
            if (sea != null)
            {
                using (DBCentre db = new DBCentre())
                {
                    var bureau = db.DataContext.Bureaus.FirstOrDefault(p => p.Guid == sea.BGuid);
                    if (bureau != null)
                    {
                        var n = 0;
                        IQueryable<RB4SampleBureau> q = db.DataContext.RB4SampleBureau;
                        q = q.Where(p => p.ArchiveGuid == sea.ArchiveGuid);
                        q = q.Where(p => p.PID == bureau.ID || p.BureauID == bureau.ID);
                        if (!string.IsNullOrEmpty(sea.Key))
                            q = q.Where(p => p.EduName == sea.Key || p.Code == sea.Key);
                        q = q.OrderBy(p => p.Code);
                        if (sea.PageSize > 0)
                        {
                            n = q.Count();
                            q = q.Skip(sea.PageSize * sea.PageIndex).Take(sea.PageSize);
                        }
                        var lst = q.ToList();
                        ret = Convertor.ConvertCollection<B4SampleBureauModel, RB4SampleBureau, B4SampleBureauCollection>(lst);
                        ret.TotalRecordsCount = n == 0 ? lst.Count : n;
                    }
                }
            }
            return ret;
        }
        /// <summary>
        /// 分析学校的样本采集进度
        /// </summary>
        /// <param name="sea"></param>
        /// <returns></returns>
        public static B4SampleSchoolCollection GetRB4SampleSchool(B4SampleSchoolQuery sea)
        {
            var ret = new B4SampleSchoolCollection();
            if (sea != null)
            {
                using (DBCentre db = new DBCentre())
                {
                    var bureau = db.DataContext.Bureaus.FirstOrDefault(p => p.Guid == sea.BGuid);
                    if (bureau != null)
                    {
                        var n = 0;
                        IQueryable<RB4SampleSchool> q = db.DataContext.RB4SampleSchool;
                        q = q.Where(p => p.ArchiveGuid == sea.ArchiveGuid);
                        var sid = bureau.SchoolInBureaux.Select(p => p.SchoolID).ToList();
                        q = q.Where(p => sid.Contains(p.SchoolID));
                        if (sea.Finish != null)
                        {
                            if (sea.Finish.Value)
                                q = q.Where(p => p.Balance == 0);
                            else
                                q = q.Where(p => p.Balance > 0);
                        }
                        if (!string.IsNullOrEmpty(sea.Key))
                            q = q.Where(p => p.SchoolName == sea.Key || p.SchoolCode == sea.Key);
                        q = q.OrderBy(p => p.SchoolCode);
                        if (sea.PageSize > 0)
                        {
                            n = q.Count();
                            q = q.Skip(sea.PageSize * sea.PageIndex).Take(sea.PageSize);
                        }
                        var lst = q.ToList();
                        ret = Convertor.ConvertCollection<B4SampleSchoolModel, RB4SampleSchool, B4SampleSchoolCollection>(lst);
                        ret.TotalRecordsCount = n == 0 ? lst.Count : n;
                    }
                }
            }
            return ret;
        }
        /// <summary>
        /// 按年级分析
        /// </summary>
        /// <param name="sea"></param>
        /// <returns></returns>
        public static B4GradeCollection GetRB4Grade(BAnalysisQuery sea)
        {
            var ret = new B4GradeCollection();
            if (sea != null)
            {
                using (DBCentre db = new DBCentre())
                {
                    var n = 0;
                    IQueryable<RB4Grade> q = db.DataContext.RB4Grade;
                    q = q.Where(p => p.ArchiveGuid == sea.ArchiveGuid);
                    if (sea.BGuid != Guid.Empty)
                        q = q.Where(p => p.BGuid == sea.BGuid);
                    q = q.OrderBy(p => p.GradeCode);
                    if (sea.PageSize > 0)
                    {
                        n = q.Count();
                        q = q.Skip(sea.PageSize * sea.PageIndex).Take(sea.PageSize);
                    }
                    var lst = q.ToList();
                    ret = Convertor.ConvertCollection<B4GradeModel, RB4Grade, B4GradeCollection>(lst);
                    ret.TotalRecordsCount = n == 0 ? lst.Count : n;
                    
                }
            }
            return ret;
        }
    }
}
